package cn.edu.zpark.security.springmvc.config;

import cn.edu.zpark.security.springmvc.interceptor.SimpleAuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
 * @PackageName: cn.edu.zpark.security.springmvc.config
 * @ClassName: WebConfig
 * @Description:
 * @author: RZS
 * @date: 2021/7/20  10:40
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "cn.edu.zpark.security.springmvc"
        ,includeFilters = { // 配置需要扫描的注解
            @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)})
public class WebConfig implements WebMvcConfigurer {

    //视频解析器
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");// 视图解析器前缀
        viewResolver.setSuffix(".jsp"); // 视图解析器后缀
        return viewResolver;
    }

    // 项目启动默认（根）的访问页面
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
    }

    /**
     * 注册拦截器
     */
    @Autowired
    private SimpleAuthenticationInterceptor simpleAuthenticationInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 拦截 /user/r路径下的所有请求
        registry.addInterceptor(simpleAuthenticationInterceptor).addPathPatterns("/user/r/**");
    }
}
